package com.zhh.service.impl;

import com.zhh.common.ResponseCode;
import com.zhh.common.ServerResponse;
import com.zhh.dao.FacilityDao;
import com.zhh.dao.FacilityLeaseDao;
import com.zhh.dao.UserDao;
import com.zhh.pojo.TbFacility;
import com.zhh.pojo.TbFacilityLease;
import com.zhh.pojo.TbUser;
import com.zhh.service.FacilityLeaseService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.UUID;


@Service
public class FacilityLeaseServiceImpl implements FacilityLeaseService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private FacilityLeaseDao leaseDao;

    @Autowired
    private FacilityDao facilityDao;

    @Override
    @Transactional
    public ServerResponse insert(String suid) {
        TbUser user = getUser();

        TbFacility facility =  facilityDao.findTbFacilityBySuidEquals(suid);
        if (facility ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"suid错误");
        }
        if (facility.getState() !=0){
            return ServerResponse.createByErrorCodeMessage(10002,"未达到使用状态或已被使用");
        }
        TbFacilityLease lease = new TbFacilityLease();
        lease.setFacilityId(facility.getId());
        lease.setId(UUID.randomUUID().toString());
        lease.setUserId(user.getId());
        lease.setState(0);
        leaseDao.save(lease);
        facilityDao.updateState(facility.getId(),10);
        return ServerResponse.createBySuccessMessage("领取成功");
    }

    @Override
    @Transactional
    public ServerResponse returnFacility(String suid) {
        TbUser user = getUser();
        TbFacility facility =  facilityDao.findTbFacilityBySuidEquals(suid);
        if (facility ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"suid错误");
        }
        TbFacilityLease lease = leaseDao.findTopByFacilityIdAndState(facility.getId(),0);
        if (lease ==null){
            return ServerResponse.createByErrorCodeMessage(10002,"物品已归还");
        }
        if (user.getId() != lease.getUserId()){
            return ServerResponse.createByErrorCodeMessage(402,"不是您的物品,您无权限归还");
        }
        facilityDao.updateState(facility.getId(),0);
        leaseDao.updateStateById(lease.getId(),10);
        return ServerResponse.createBySuccessMessage("归还成功");

    }

    @Override
    public ServerResponse storage(String suid) {
        TbUser user = getUser();
        TbFacility facility =  facilityDao.findTbFacilityBySuidEquals(suid);
        if (facility ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"suid错误");
        }
//        facilityDao.updateState(facility.getId(),0);
        return null;
    }

    @Override
    public ServerResponse returnFacilityByOrder(String orderId) {
        return null;
    }

    private TbUser getUser(){
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        return  userDao.findbyUsername(username);
    }

}
